package com.foxzzz.dynamic.programming;


public class Solution279 {
    public int numSquares(int n) {
        int dp[] = new int[n + 1];
        dp[0] = 0;
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            int min = Integer.MAX_VALUE;
            for (int x =i/2; x <i ; x++) {
                if ((i-x) * (i-x) == i) {
                    min = 1;
                    break;
                }
                min = Math.min(min, dp[(i-x)] + dp[x]);
            }
            dp[i] = min;
        }

        return dp[n];
    }

    public int numSquares1(int n) {
        int dp[] = new int[n + 1];
        dp[0] = 0;
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = 1; j * j <= i; j++) {
                if (j * j == i) {
                    min = 1;
                    break;
                }
                min = Math.min(min, dp[j * j] + dp[i - j * j]);
            }
            dp[i] = min;
        }

        return dp[n];
    }

    public static void main(String[] args) {
        //3
        System.out.println(new Solution279().numSquares(12));
        //2
        System.out.println(new Solution279().numSquares(13));

    }
}
